package top.huhuiyu.springboot3.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 工具类
 *
 * @author 胡辉煜
 */
@Slf4j
public class Tools {
  public static final String CODES = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  public static final int MIN_LENGTH = 1;
  public static final int MAX_LENGTH = 6;
  public static final int VALIDATE_CODE_MAX = 999999;
  public static final int VALIDATE_CODE_MIN = 100000;
  private final static Random random = new Random();
  private final static String LIKE = "%%%s%%";
  private final static String LIKE_LEFT = "%%%s";
  private final static String LIKE_RIGHT = "%s%%";
  public static final int CHINESE_START = 0x4e00;
  public static final int CHINESE_END = 0x9fa5;
  public static String COMMON_CHARS = "的一是在不了有和人这中大为上个国我以要他时来用们生到作地于出就分对成会可主发年动同工也能下过子说产种面而方后多定行学法所民得经十三之进着等部度家电力里如水化高自二理起小物现实加量都两体制机当使点从业本去把性好应开它合还因由其些然前外天政四日那社义事平形相全表间样与关各重新线内数正心反你明看原又么利比或但质气第向道命此变条只没结解问意建月公无系军很情者最立代想已通并提直题党程展五果料象员革位入常文总次品式活设及管特件长求老头基资边流路级少图山统接知较将组见计别她手角期根论运农指几九区强放决西被干做必战先回则任取据处队南给色光门即空互治北造百规热领七海口东导器压志世金增争济阶油思术极交受联什认六共权收证改清己美再采转更单风切打白教速花带安场身车例真务具万每目至达走积示议声报斗完类八离华名确才科张信马节话米整空元况今集温传土许步群广石记需段研界拉林律叫且究观越织装影算低持音众书布复容儿须际商非验连断深难近矿千周委素技备半办青省列习响约支般史感劳便团往酸历市克何除消构府称太准精值号率族维划选标写存候毛亲快效斯院查江型眼王按格养易置派层片始却专状育厂京识适属圆包火住调满县局照参红细引听该铁价严龙飞";


  /**
   * 拼接like字符
   *
   * @param value 要拼接的字符
   * @return like字符
   */
  public static String like(String value) {
    if (StringUtils.hasText(value)) {
      return String.format(LIKE, value);
    } else {
      return "";
    }
  }

  /**
   * 拼接左like字符
   *
   * @param value 要拼接的字符
   * @return 左like字符
   */
  public static String likeLeft(String value) {
    if (StringUtils.hasText(value)) {
      return String.format(LIKE_LEFT, value);
    } else {
      return "";
    }
  }

  /**
   * 拼接右like字符
   *
   * @param value 要拼接的字符
   * @return 右like字符
   */
  public static String likeRight(String value) {
    if (StringUtils.hasText(value)) {
      return String.format(LIKE_RIGHT, value);
    } else {
      return "";
    }
  }

  /**
   * 生成随机码
   *
   * @param length 随机码长度
   * @return 随机码
   */
  public static String makeCode(int length) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < length; i++) {
      sb.append(CODES.charAt(random.nextInt(CODES.length())));
    }
    return sb.toString();
  }

  /**
   * 生成指定长度的前导补0的随机数字符串
   *
   * @param length 数字的长度
   * @return 指定长度的前导补0的随机数字符串
   */
  public static String makeRandomNumber(int length) {
    if (length < MIN_LENGTH || length > MAX_LENGTH) {
      throw new IllegalArgumentException(String.format("length必须是%s到%s", MIN_LENGTH, MAX_LENGTH));
    }
    int max = (int) Math.pow(10, length);
    return String.format("%0" + length + "d", random.nextInt(max));
  }

  /**
   * 生成指定范围的随机数
   *
   * @param min 下届
   * @param max 上届
   * @return 指定范围的随机数
   */
  public static int makeRandomNumber(int min, int max) {
    return random.nextInt(max - min + 1) + min;
  }

  /**
   * 正则校验
   *
   * @param info 校验值
   * @param reg  正则
   * @return 正则校验结果
   */
  public static boolean regInfoNotChecked(String info, String reg) {
    if (!StringUtils.hasText(reg)) {
      return true;
    }
    if (StringUtils.hasText(info)) {
      return !Pattern.matches(reg, info);
    }
    return true;
  }

  /**
   * 生产随机校验码
   *
   * @return 随机校验码
   */
  public static String makeValidateCode() {
    return (random.nextInt((VALIDATE_CODE_MAX - VALIDATE_CODE_MIN) + 1) + VALIDATE_CODE_MIN) + "";
  }

  /**
   * 生成随机中文
   *
   * @param length 字数，只能是1-100
   * @param common 是否只包含常用中文
   * @return 随机中文
   */
  public static String makeRandChinese(int length, boolean common) {
    length = length < 1 || length > 100 ? 10 : length;
    Random random = new Random();
    Set<Character> set = new HashSet<>();
    int start = CHINESE_START;
    int end = CHINESE_END;
    while (set.size() < length) {
      if (common) {
        set.add(COMMON_CHARS.charAt(random.nextInt(COMMON_CHARS.length())));
      } else {
        int codePoint = random.nextInt(end - start + 1) + start;
        set.add((char) codePoint);
      }
    }
    return set.stream().map(String::valueOf).collect(Collectors.joining());
  }

  /**
   * 生成随机常用中文
   *
   * @param length 字数
   * @return 随机中文
   */
  public static String makeRandChinese(int length) {
    return makeRandChinese(length, false);
  }

  /**
   * 随机打乱字符串
   *
   * @param input 要打乱的字符串
   * @return 打乱后的字符串
   */
  public static String shuffleString(String input) {
    // 将字符串转换为字符数组
    Character[] chars = new Character[input.length()];
    for (int i = 0; i < input.length(); i++) {
      chars[i] = input.charAt(i);
    }

    // 使用Collections.shuffle()方法对字符数组进行随机排序
    List<Character> charList = Arrays.asList(chars);
    Collections.shuffle(charList);

    // 将字符数组转换回字符串
    StringBuilder shuffled = new StringBuilder(input.length());
    for (Character c : charList) {
      shuffled.append(c);
    }
    return shuffled.toString();
  }

  /**
   * 生成一个随机的uuid
   *
   * @return uuid
   */
  public static String uuid() {
    String uuid = UUID.randomUUID().toString();
    return uuid.replace("-", "");
  }

}
